﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Sec;

namespace ESOL.WinUI.Sec
{
    public partial class frmMaintainUser : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public SecUserEntity UserEntity { get; set; }
        #endregion

        #region Constructor
        public frmMaintainUser()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetFields()
        {
            if (UserEntity == null)
            {
                return;
            }
            txtUserAccount.Text = UserEntity.UserAccount;
            txtChineseName.Text = UserEntity.ChineseName;
            txtEnglishName.Text = UserEntity.EnglishName;
            txtEmail.Text = UserEntity.Email;
            cboxDeleted.Checked = false;
            if (UserEntity.IsDelete == 1)
            {
                cboxDeleted.Checked = true;
            }
            cboxLocked.Checked = false;
            if (UserEntity.IsLock == 1)
            {
                cboxLocked.Checked = true;
            }
        }

        private void SetEntity()
        {
            if (UserEntity == null)
            {
                UserEntity = new SecUserEntity();
                UserEntity.UserId = Guid.NewGuid().ToString();
                UserEntity.Password = Common.Encryption.Encrypt(CommonParameter.DefaultPassword);
                UserEntity.PasswordModifyDate = DateTime.Now;
                UserEntity.UserType = 0;
                UserEntity.Gender = 0;
                UserEntity.ValidateType = 0;
                UserEntity.PersonId = "1";
            }
            UserEntity.UserAccount = txtUserAccount.Text;
            UserEntity.ChineseName = txtEnglishName.Text;
            UserEntity.EnglishName = txtEnglishName.Text;
            UserEntity.Email = txtEmail.Text;
            UserEntity.IsDelete = 0;
            UserEntity.IsLock = 0;
            if (cboxLocked.Checked)
            {
                UserEntity.IsLock = 1;
            }
            UserEntity.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            UserEntity.LastModifiedTime = DateTime.Now;
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = SecUserBs.InsertUser(UserEntity);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("Insert failed!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = SecUserBs.UpdateUser(UserEntity);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("Modify failed!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtUserAccount.Text))
            {
                MessageBox.Show("User accoount can't be null!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtEmail.Text))
            {
                MessageBox.Show("User email can't be null!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        #endregion

        #region Event
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void frmMaintainUser_Load(object sender, EventArgs e)
        {
            SetFields();
        }
        #endregion
    }
}
